#include "gtlOptimization.h"

namespace GTL
{
	namespace Engine
	{
		namespace Profiling
		{
			void Function::Tick( int line, const char* name /*= ""*/ )
			{
				uint64_t t = Backend->GetTimeMsec()-StartTime;

				ProfileLine& pl = Lines[line];
				pl.Line = line;
				pl.Count++;
				if (t>pl.HighestTime)
					pl.HighestTime = t;
				if (t<pl.LowestTime)
					pl.LowestTime = t;
				pl.TotalTime += t;
				pl.Description = name;
			}

			void Function::Output()
			{
				printf(">> Profile for function `%s` in file `%s` @ line %i\n", FuncName, FileName, StartLine);
				printf("| Line\t| Count\t| High\t| Low\t| Total\t| Avg\t| Desc\t\t\t|\n");
				printf("---------------------------------------------------------------------------\n");
				for (eastl::map<int,ProfileLine>::iterator i=Lines.begin(); i!=Lines.end(); i++)
				{
					ProfileLine& pl = i->second;
					printf("| %i\t| %i\t| %li\t| %li\t| %li\t| %li\t| %s\t\t\t|\n", pl.Line, pl.Count, pl.HighestTime, pl.LowestTime, pl.TotalTime, pl.TotalTime/pl.Count, pl.Description);
				}
			}
		}
	}

}